# !/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time   :  2019/7/4 10:50
@Author :  geqh
@file   :  api.py
"""
from flask import Flask, request
from language import lang_recognition
from utils.result import *
from utils.log import *
from utils.confPaser import *
import json

app = Flask(__name__)


# 接口helloCN
@app.route('/language', methods=['GET', 'POST'])
def language():
    try:  # 必要参数
        text = request.values['text']
    except BaseException:
        errMsg = 'parameter[text] is lost'
        res_fail = result(False, errMsg, None)
        res = json.dumps(obj=res_fail.__dict__, ensure_ascii=False)
        logger.error(errMsg)
        return res
    logger.info('language recognition parameter text:' + text)
    res = lang_recognition.language(text)
    res_suc = result(True, None, res)
    res = json.dumps(obj=res_suc.__dict__, ensure_ascii=False)
    return res


# 可直接测试
if __name__ == '__main__':
    port = cp.get("common", "port")
    logger.info("port in conf:" + port)
    port = int(port)
    app.run(debug=False, host='0.0.0.0', port=port)
